﻿using Microsoft.AspNetCore.Mvc;

namespace ModelBinder.Web.Controllers;

public class HomeController : Controller
{
    public IActionResult CreateMultiParam(List<TestModel> sources, List<TestModel> targets)
    {
        sources.AddRange(targets);
        return new JsonResult(sources);
    }

    /// <summary>
    /// 该种场景下，最好不要取名id, 传参绑定容易失效报错
    /// Property on type 'IdModel' has the same names as parameter 'id'
    /// This may result in incorrect model binding
    /// Consider renaming the parameter or the property to avoid conflicts
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    //public IActionResult Create(IdModel id)
    //{
    //    return new JsonResult(idModel);
    //}

    public IActionResult Create(IdModel idModel)
    {
        return new JsonResult(idModel);
    }

    public IActionResult SingleValue([FromCustomJsonValue] int id)
    {
        return new JsonResult(id);
    }

    public IActionResult SingleList([FromCustomJsonValue] List<int> ids)
    {
        return new JsonResult(ids);
    }

    /// <summary>
    /// 不加入自定义模型绑定机制下，也是需要FromBody才能绑定来自Body中的数据
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public IActionResult NormalSingleValue([FromBody] int id)
    {
        return new JsonResult(id);
    }

    public IActionResult NormalSingleList(List<int> ids)
    {
        return new JsonResult(ids);
    }

    public IActionResult CreateEmptyBody(TestModel testModel)
    {
        return new JsonResult(testModel);
    }

    public IActionResult CreateArray(ArrayModel arrayModel)
    {
        return new JsonResult(arrayModel);
    }

    public IActionResult CreateEmptyArray(ArrayModel arrayModel)
    {
        return new JsonResult(arrayModel);
    }

    public IActionResult CreateNullCollection(CollectionModel collectionModel)
    {
        return new JsonResult(collectionModel);
    }

    [HttpPost("Home/{id}/CreateRouteValue")]
    public IActionResult CreateRouteValue(RouteModel routeModel)
    {
        return new JsonResult(routeModel);
    }
}

public class TestModel
{
    public int Id { get; set; }
}

public class IdModel
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class ArrayModel
{
    public short[] StatusIds { get; set; }
    public string Name { get; set; }
}

public class CollectionModel
{
    public List<CollectionItemModel> CollectionItemModels { get; set; }
}

public class CollectionItemModel
{
    public int Id { get; set; }
}

public class RouteModel
{
    public string Name { get; set; }
    [FromRoute]
    public string Id { get; set; }
}